package cn.wenzy.aurora.admin.oauth2.userdetails;

import cn.hutool.core.util.ObjectUtil;
import cn.wenzy.aurora.admin.modules.organization.bean.SysUser;
import cn.wenzy.aurora.admin.modules.organization.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class SysUserDetailsService implements UserDetailsService {

    @Autowired
    private SysUserService sysUserService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser = sysUserService.getById(username);
        if (ObjectUtil.isNull(sysUser)) {
            throw new UsernameNotFoundException("");
        }
        return User.withUsername(username)
                .password(sysUser.getPassword())
                .authorities(AuthorityUtils.NO_AUTHORITIES)
                .build();
    }

}
